package com.boby.homecamera.service


import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.util.Log
import com.boby.homecamera.R
import com.boby.homecamera.constant.Constant
import com.boby.homecamera.service.server.RecordServer
import com.boby.homecamera.webserver.WebServerManager
import com.boby.homecamera.webserver.websocket.MyWebSocketServer
import com.k2fsa.sherpa.onnx.audio.tagging.Tagger
import kotlin.concurrent.thread


class MainService : Service() {
    companion object{
        val NOTIFICATION_ID: Int = 1
        val CHANNEL_ID: String = "recording_channel"
        private val TAG = "MainServer"

        var isRunning = false
            private set
         var recordServer : RecordServer? = null
             private set
         var webServerManager: WebServerManager? = null
             private set
        var webSocketServer: MyWebSocketServer? = null
            private set
    }


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notificationBuilder: Notification.Builder
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, "Home Camera", NotificationManager.IMPORTANCE_HIGH)
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(channel)
            notificationBuilder = Notification.Builder(this, CHANNEL_ID)
        }else{
            notificationBuilder = Notification.Builder(this)
        }

        notificationBuilder.setContentTitle("Home Camera")
            .setContentText("正在运行")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build()
        Notification.Builder(this)


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            startForeground(NOTIFICATION_ID, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
        }else{
            startForeground(NOTIFICATION_ID, notificationBuilder.build())
        }

        Log.d(TAG,"onStartCommand")
        // Your recording logic here
        return START_STICKY
    }
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG,"onCreate")
        init()
    }

    override fun onDestroy() {
        super.onDestroy()
        isRunning = false
        recordServer?.stop()
        recordServer = null
        webServerManager?.stop()
        webServerManager = null
        try {
            webSocketServer?.stop()
        }catch (e:Exception){
            e.printStackTrace()
        }

        webSocketServer = null

    }

    private fun init(){
        thread {
            Tagger.initTagger(this.assets)
            recordServer = RecordServer(this)
            recordServer?.start()


            webServerManager = WebServerManager()
            webServerManager?.init(this)
            webServerManager?.start()

            webSocketServer = MyWebSocketServer(Constant.WEBSOCKET_PORT)
            webSocketServer?.start()


            isRunning = true
        }



    }
}